package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.tb.lmt.mapper.TableColumnSyncMapper;
import com.ruoyi.tb.lmt.service.ITableColumnSyncService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: jiaxiufeng
 * @description:
 * @create: 2023/11/28 08:44
 **/
@RequiredArgsConstructor
@Service
public class TableColumnSyncService implements ITableColumnSyncService {
    @Autowired
    TableColumnSyncMapper tableColumnSyncMapper;

    @Override
    public Map<String, Object> sync() {
        /**
         * 获取tb_与t_开头的表
         * 循环这些表的字段，字段类型，字段注释
         * 将字段首字母变小写并将驼峰替换为‘_’
         * 将 CreateTime 以下的四个字段做好对应
         */
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> tables = tableColumnSyncMapper.getTables();
        for (int i = 0; i < tables.size(); i++) {
            Map<String, Object> resColumns = new HashMap<>();
            String tableName = (String) tables.get(i).get("TABLE_NAME");
            List<Map<String, Object>> columns = tableColumnSyncMapper.getColumnByTableName(tableName);
            for (int j = 0; j < columns.size(); j++) {
                String oldColumnName = (String) columns.get(j).get("COLUMN_NAME");
                String newColumn;
                if (!StrUtil.isUpperCase(oldColumnName)) {
                    newColumn = StrUtil.lowerFirst(oldColumnName);
                    newColumn = StrUtil.toUnderlineCase(newColumn);
                    switch (newColumn) {
                        case "create_user":
                            newColumn = "create_by";
                            break;
                        case "update_user":
                            newColumn = "update_by";
                            break;
                        default:
                            break;
                    }
                } else {
                    newColumn = oldColumnName;
                }
                //获取下划线出现的位置
                int index = newColumn.indexOf("_");
                if (index ==1 && StrUtil.isUpperCase(newColumn.substring(index+1,index+2))) {
                    //删除该位置下划线
                    newColumn = newColumn.substring(0,index) + newColumn.substring(index+1);
                    newColumn = StrUtil.upperFirst(newColumn);
                }
                if (!oldColumnName.equals(newColumn)) {
                    Map<String,String> map = new HashMap<>();
                    map.put("tableName",tableName);
                    map.put("oldColumnName",oldColumnName);
                    map.put("newColumn",newColumn);
                    tableColumnSyncMapper.alertTable(map);
                    resColumns.put(oldColumnName, newColumn);
                }
            }
            res.put(tableName, resColumns);
        }
        return res;
    }

    @Override
    public Map<String, Object> commit() {
        /**
         * 获取tb_与t_开头的表
         * 循环这些表的字段，字段类型，字段注释
         * 将 CreateTime 四个字段加注释
         */
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> tables = tableColumnSyncMapper.getTables();
        for (int i = 0; i < tables.size(); i++) {
            Map<String, Object> resColumns = new HashMap<>();
            String tableName = (String) tables.get(i).get("TABLE_NAME");
            List<Map<String, Object>> columns = tableColumnSyncMapper.getColumnByTableName(tableName);
            for (int j = 0; j < columns.size(); j++) {
                String columnName = (String) columns.get(j).get("COLUMN_NAME");
                String commit = "";
                    switch (columnName) {
                        case "create_by":
                            commit = "创建人";
                            break;
                        case "update_by":
                            commit = "修改人";
                            break;
                        case "update_time":
                            commit = "修改时间";
                            break;
                        case "create_time":
                            commit = "创建时间";
                            break;
                        default:
                            break;
                    }
                    if (columnName.equals("create_by") || columnName.equals("update_by")){
                        Map<String,String> map = new HashMap<>();
                        map.put("tableName",tableName);
                        map.put("columnName",columnName);
                        map.put("commit",commit);
                        tableColumnSyncMapper.alertColumnCommit(map);
                        resColumns.put(columnName, commit);
                    }
                    if (columnName.equals("create_time") || columnName.equals("update_time")){
                        Map<String,String> map = new HashMap<>();
                        map.put("tableName",tableName);
                        map.put("columnName",columnName);
                        map.put("commit",commit);
                        tableColumnSyncMapper.alertTimeCommit(map);
                        resColumns.put(columnName, commit);
                    }
                }
            res.put(tableName, resColumns);
        }
        return res;
    }
}
